package com.school.support.listener;

import com.school.food.domain.Config;
import com.school.food.service.ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * Created by 执笔 on 2017/3/15.
 */
public class WebConfigListener implements ServletContextListener {

    private Logger logger = LoggerFactory.getLogger(WebConfigListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.findWebApplicationContext(servletContext);
        ConfigService sysConfigService = webApplicationContext.getBean(ConfigService.class);
        List<Config> sysConfigs = sysConfigService.selectAll();
        for (Config config : sysConfigs) {
            servletContext.setAttribute("web_" + config.getNid(), config.getValue());
           // ContextConfig.putConfig(config.getNid(), config.getValue());
            logger.info("【系 统 启 动】 {} : {} --- {}", config.getName(), config.getNid(), config.getValue());
        }
      //  ContextConfig.servletContext = servletContext;
    }

}
